<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/limh/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/limh/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/limh/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/limh/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/limh/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/limh/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/limh/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Flutter,">










<meta name="description" content="前言需求情景:  需要实现一个类似闲鱼APP的底部导航栏的实现，Flutter上实现方式有两种：  BottomNavigationBar BottomAppBar  思路不论用哪种方式实现，底部总共五个按钮，可以在底部平均分布5个同样大小的按钮，再在中间放一个FloatingActionButton，将floatingActionButtonLocation 设置为FloatingActionB">
<meta name="keywords" content="Flutter">
<meta property="og:type" content="article">
<meta property="og:title" content="Flutter底部凸起导航栏">
<meta property="og:url" content="https://tdnr.gitee.io/limh/2019/05/30/flutter4/index.html">
<meta property="og:site_name" content="土逗牛肉">
<meta property="og:description" content="前言需求情景:  需要实现一个类似闲鱼APP的底部导航栏的实现，Flutter上实现方式有两种：  BottomNavigationBar BottomAppBar  思路不论用哪种方式实现，底部总共五个按钮，可以在底部平均分布5个同样大小的按钮，再在中间放一个FloatingActionButton，将floatingActionButtonLocation 设置为FloatingActionB">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/10147880-053cb5ee35db4c2a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/10147880-4e3948254d4b4b60.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2019-05-30T14:57:31.302Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Flutter底部凸起导航栏">
<meta name="twitter:description" content="前言需求情景:  需要实现一个类似闲鱼APP的底部导航栏的实现，Flutter上实现方式有两种：  BottomNavigationBar BottomAppBar  思路不论用哪种方式实现，底部总共五个按钮，可以在底部平均分布5个同样大小的按钮，再在中间放一个FloatingActionButton，将floatingActionButtonLocation 设置为FloatingActionB">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/10147880-053cb5ee35db4c2a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/limh/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://tdnr.gitee.io/limh/2019/05/30/flutter4/">





  <title>Flutter底部凸起导航栏 | 土逗牛肉</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans ">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/limh/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">土逗牛肉</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/limh/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/limh/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/limh/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/limh/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/limh/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://tdnr.gitee.io/limh/limh/2019/05/30/flutter4/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="土逗牛肉">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/limh/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="土逗牛肉">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Flutter底部凸起导航栏</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-05-30T20:35:45+08:00">
                2019-05-30
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/limh/categories/Flutter/" itemprop="url" rel="index">
                    <span itemprop="name">Flutter</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>需求情景:  需要实现一个类似闲鱼APP的底部导航栏的实现，Flutter上实现方式有两种：</p>
<ul>
<li>BottomNavigationBar</li>
<li>BottomAppBar</li>
</ul>
<h2 id="思路"><a href="#思路" class="headerlink" title="思路"></a>思路</h2><p>不论用哪种方式实现，底部总共五个按钮，可以在底部平均分布5个同样大小的按钮，再在中间放一个<strong>FloatingActionButton</strong>，将<strong>floatingActionButtonLocation</strong> 设置为<strong>FloatingActionButtonLocation.centerDocked</strong>遮住中间的按钮，达到中部凸起的效果。</p>
<h3 id="BottomNavigationBar实现"><a href="#BottomNavigationBar实现" class="headerlink" title="BottomNavigationBar实现"></a>BottomNavigationBar实现</h3><p>用BottomNavigationBar实现比较简单，代码如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br></pre></td><td class="code"><pre><span class="line">import &apos;package:flutter/material.dart&apos;;</span><br><span class="line">import &apos;package:flutter/services.dart&apos;;</span><br><span class="line">import &apos;package:flutter_chip/utils/toast_utils.dart&apos;;</span><br><span class="line"></span><br><span class="line">import &apos;view/view_home.dart&apos;;</span><br><span class="line">import &apos;view/view_forum.dart&apos;;</span><br><span class="line">import &apos;view/view_mine.dart&apos;;</span><br><span class="line">import &apos;view/view_news.dart&apos;;</span><br><span class="line"></span><br><span class="line">void main() =&gt; runApp(MyApp());</span><br><span class="line"></span><br><span class="line">class MyApp extends StatelessWidget &#123;</span><br><span class="line">  @override</span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    return MaterialApp(</span><br><span class="line">      theme: ThemeData(primarySwatch: Colors.deepOrange),</span><br><span class="line">      home: MyHomePage(),</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class MyHomePage extends StatefulWidget &#123;</span><br><span class="line">  @override</span><br><span class="line">  _MyHomePageState createState() =&gt; _MyHomePageState();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class _MyHomePageState extends State&lt;MyHomePage&gt; &#123;</span><br><span class="line">  int lastTime = 0;</span><br><span class="line">  PageController pageController;</span><br><span class="line">  int page = 0;</span><br><span class="line"></span><br><span class="line">  @override</span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    return WillPopScope(</span><br><span class="line">        child: Scaffold(</span><br><span class="line">            body: PageView(</span><br><span class="line">              children: &lt;Widget&gt;[</span><br><span class="line">                HomePage(),</span><br><span class="line">                ForumPage(),</span><br><span class="line">                NewsPage(),</span><br><span class="line">                MinePage()</span><br><span class="line">              ],</span><br><span class="line">              onPageChanged: onPageChanged,</span><br><span class="line">              controller: pageController,</span><br><span class="line">            ),</span><br><span class="line">            floatingActionButton: FloatingActionButton(</span><br><span class="line">              onPressed: () &#123;&#125;,</span><br><span class="line">              child: Icon(</span><br><span class="line">                Icons.add,</span><br><span class="line">                color: Colors.white,</span><br><span class="line">              ),</span><br><span class="line">            ),</span><br><span class="line">            floatingActionButtonLocation:</span><br><span class="line">                FloatingActionButtonLocation.centerDocked,</span><br><span class="line">            bottomNavigationBar: BottomNavigationBar(</span><br><span class="line">                items: [</span><br><span class="line">                  BottomNavigationBarItem(</span><br><span class="line">                      icon: Icon(Icons.home), title: Text(&apos;首页&apos;)),</span><br><span class="line">                  BottomNavigationBarItem(</span><br><span class="line">                      icon: Icon(Icons.store), title: Text(&apos;论坛&apos;)),</span><br><span class="line">                  BottomNavigationBarItem(</span><br><span class="line">                      icon: Icon(Icons.home), title: Text(&apos;发布&apos;)),</span><br><span class="line">                  BottomNavigationBarItem(</span><br><span class="line">                      icon: Icon(Icons.home), title: Text(&apos;消息&apos;)),</span><br><span class="line">                  BottomNavigationBarItem(</span><br><span class="line">                      icon: Icon(Icons.home), title: Text(&apos;我的&apos;)),</span><br><span class="line">                ],</span><br><span class="line">                type: BottomNavigationBarType.fixed,</span><br><span class="line">                fixedColor: Theme.of(context).primaryColor,</span><br><span class="line">                onTap: onTap,</span><br><span class="line">                currentIndex: page)</span><br><span class="line">            ),</span><br><span class="line">        onWillPop: () &#123;</span><br><span class="line">          int newTime = DateTime.now().millisecondsSinceEpoch;</span><br><span class="line">          int result = newTime - lastTime;</span><br><span class="line">          lastTime = newTime;</span><br><span class="line">          if (result &gt; 2000) &#123;</span><br><span class="line">            Toast.show(context, &apos;再按一次退出&apos;);</span><br><span class="line">          &#125; else &#123;</span><br><span class="line">            SystemNavigator.pop();</span><br><span class="line">          &#125;</span><br><span class="line">          return null;</span><br><span class="line">        &#125;);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @override</span><br><span class="line">  void initState() &#123;</span><br><span class="line">    super.initState();</span><br><span class="line">    pageController = PageController(initialPage: this.page);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  void onPageChanged(int value) &#123;</span><br><span class="line">    if (value &gt;= 2) &#123;</span><br><span class="line">      value = value + 1;</span><br><span class="line">    &#125;</span><br><span class="line">    setState(() &#123;</span><br><span class="line">      this.page = value;</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br><span class="line">  void onTap(int value) &#123;</span><br><span class="line">    if (value == 2) return;</span><br><span class="line">    if (value &gt; 2) &#123;</span><br><span class="line">      value = value - 1;</span><br><span class="line">    &#125;</span><br><span class="line">    pageController.animateToPage(value,</span><br><span class="line">        duration: const Duration(milliseconds: 100), curve: Curves.ease);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>用<strong>BottomNavigationBar</strong>的好处就是按钮选中变化状态不用我们处理，组件已经封装好了，唯一需要处理的地方就是每个item按钮都对应一个页面，我们总共4个页面却对应5个按钮，所以在我们要去除中间按钮选中和点击的功能，那么就得在<strong>onTap</strong>和<strong>onPageChanged</strong>里面处理，不要忘记中间按钮图标色改成透明，免得不用尺寸屏幕把中间底部按钮露出来。实现效果如下：</p>
<p><img src="https://upload-images.jianshu.io/upload_images/10147880-053cb5ee35db4c2a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="device-2019-05-30-224610.png"></p>
<h3 id="BottomAppBar"><a href="#BottomAppBar" class="headerlink" title="BottomAppBar"></a>BottomAppBar</h3><p>用<strong>BottomAppBar</strong>的话导航栏每个图标都需要自己实现选中和未选中动态效果，还需要给每个按钮添加点击事件，比较繁琐。但是<strong>BottomAppBar</strong>比较灵活，可以实现很炫酷的效果，如下图：</p>
<p><img src="https://upload-images.jianshu.io/upload_images/10147880-4e3948254d4b4b60.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="device-2019-05-30-224115.png"></p>
<p>代码如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br></pre></td><td class="code"><pre><span class="line">import &apos;package:flutter/material.dart&apos;;</span><br><span class="line">import &apos;package:flutter/services.dart&apos;;</span><br><span class="line">import &apos;package:flutter_chip/utils/toast_utils.dart&apos;;</span><br><span class="line"></span><br><span class="line">import &apos;view/view_home.dart&apos;;</span><br><span class="line">import &apos;view/view_forum.dart&apos;;</span><br><span class="line">import &apos;view/view_mine.dart&apos;;</span><br><span class="line">import &apos;view/view_news.dart&apos;;</span><br><span class="line"></span><br><span class="line">void main() =&gt; runApp(MyApp());</span><br><span class="line"></span><br><span class="line">class MyApp extends StatelessWidget &#123;</span><br><span class="line">  @override</span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    return MaterialApp(</span><br><span class="line">      theme: ThemeData(primarySwatch: Colors.deepOrange),</span><br><span class="line">      home: MyHomePage(),</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class MyHomePage extends StatefulWidget &#123;</span><br><span class="line">  @override</span><br><span class="line">  _MyHomePageState createState() =&gt; _MyHomePageState();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class _MyHomePageState extends State&lt;MyHomePage&gt; &#123;</span><br><span class="line">  int lastTime = 0;</span><br><span class="line">  PageController pageController;</span><br><span class="line">  int page = 0;</span><br><span class="line"></span><br><span class="line">  @override</span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    return WillPopScope(</span><br><span class="line">        child: Scaffold(</span><br><span class="line">          body: PageView(</span><br><span class="line">            children: &lt;Widget&gt;[HomePage(), ForumPage(), NewsPage(), MinePage()],</span><br><span class="line">            onPageChanged: onPageChanged,</span><br><span class="line">            controller: pageController,</span><br><span class="line">          ),</span><br><span class="line">          floatingActionButton: FloatingActionButton(</span><br><span class="line">            onPressed: () &#123;&#125;,</span><br><span class="line">            child: Icon(</span><br><span class="line">              Icons.add,</span><br><span class="line">              color: Colors.white,</span><br><span class="line">            ),</span><br><span class="line">          ),</span><br><span class="line">          floatingActionButtonLocation:</span><br><span class="line">              FloatingActionButtonLocation.centerDocked,</span><br><span class="line">          bottomNavigationBar: BottomAppBar(</span><br><span class="line">            color: Theme.of(context).accentColor,</span><br><span class="line">            shape: CircularNotchedRectangle(),</span><br><span class="line">            child: Padding(</span><br><span class="line">              padding: EdgeInsets.fromLTRB(0, 6, 0, 6),</span><br><span class="line">              child: Row(</span><br><span class="line">                mainAxisSize: MainAxisSize.max,</span><br><span class="line">                mainAxisAlignment: MainAxisAlignment.spaceAround,</span><br><span class="line">                children: &lt;Widget&gt;[</span><br><span class="line">                  GestureDetector(</span><br><span class="line">                      onTap: () &#123;</span><br><span class="line">                        onTap(0);</span><br><span class="line">                      &#125;,</span><br><span class="line">                      child: Column(</span><br><span class="line">                        mainAxisSize: MainAxisSize.min,</span><br><span class="line">                        children: &lt;Widget&gt;[</span><br><span class="line">                          Icon(Icons.home, color: getColor(0)),</span><br><span class="line">                          Text(&quot;首页&quot;, style: TextStyle(color: getColor(0)))</span><br><span class="line">                        ],</span><br><span class="line">                      )),</span><br><span class="line">                  GestureDetector(</span><br><span class="line">                      onTap: () &#123;</span><br><span class="line">                        onTap(1);</span><br><span class="line">                      &#125;,</span><br><span class="line">                      child: Column(</span><br><span class="line">                        mainAxisSize: MainAxisSize.min,</span><br><span class="line">                        children: &lt;Widget&gt;[</span><br><span class="line">                          Icon(Icons.forum, color: getColor(1)),</span><br><span class="line">                          Text(&quot;论坛&quot;, style: TextStyle(color: getColor(1)))</span><br><span class="line">                        ],</span><br><span class="line">                      )),</span><br><span class="line">                  Column(</span><br><span class="line">                    mainAxisSize: MainAxisSize.min,</span><br><span class="line">                    children: &lt;Widget&gt;[</span><br><span class="line">                      Icon(</span><br><span class="line">                        Icons.home,</span><br><span class="line">                        color: Colors.transparent,</span><br><span class="line">                      ),</span><br><span class="line">                      Text(&quot;发布&quot;, style: TextStyle(color: Color(0xFFEEEEEE)))</span><br><span class="line">                    ],</span><br><span class="line">                  ),</span><br><span class="line">                  GestureDetector(</span><br><span class="line">                      onTap: () &#123;</span><br><span class="line">                        onTap(2);</span><br><span class="line">                      &#125;,</span><br><span class="line">                      child: Column(</span><br><span class="line">                        mainAxisSize: MainAxisSize.min,</span><br><span class="line">                        children: &lt;Widget&gt;[</span><br><span class="line">                          Icon(Icons.mail, color: getColor(2)),</span><br><span class="line">                          Text(&quot;消息&quot;, style: TextStyle(color: getColor(2)))</span><br><span class="line">                        ],</span><br><span class="line">                      )),</span><br><span class="line">                  GestureDetector(</span><br><span class="line">                      onTap: () &#123;</span><br><span class="line">                        onTap(3);</span><br><span class="line">                      &#125;,</span><br><span class="line">                      child: Column(</span><br><span class="line">                        mainAxisSize: MainAxisSize.min,</span><br><span class="line">                        children: &lt;Widget&gt;[</span><br><span class="line">                          Icon(Icons.person, color: getColor(3)),</span><br><span class="line">                          Text(&quot;我的&quot;, style: TextStyle(color: getColor(3)))</span><br><span class="line">                        ],</span><br><span class="line">                      ))</span><br><span class="line">                ],</span><br><span class="line">              ),</span><br><span class="line">            ),</span><br><span class="line">          ),</span><br><span class="line">        ),</span><br><span class="line">        onWillPop: () &#123;</span><br><span class="line">          int newTime = DateTime.now().millisecondsSinceEpoch;</span><br><span class="line">          int result = newTime - lastTime;</span><br><span class="line">          lastTime = newTime;</span><br><span class="line">          if (result &gt; 2000) &#123;</span><br><span class="line">            Toast.show(context, &apos;再按一次退出&apos;);</span><br><span class="line">          &#125; else &#123;</span><br><span class="line">            SystemNavigator.pop();</span><br><span class="line">          &#125;</span><br><span class="line">          return null;</span><br><span class="line">        &#125;);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @override</span><br><span class="line">  void initState() &#123;</span><br><span class="line">    super.initState();</span><br><span class="line">    pageController = PageController(initialPage: this.page);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  void onPageChanged(int value) &#123;</span><br><span class="line">    setState(() &#123;</span><br><span class="line">      this.page = value;</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  Color getColor(int value) &#123;</span><br><span class="line">    return this.page == value</span><br><span class="line">        ? Theme.of(context).cardColor</span><br><span class="line">        : Color(0XFFBBBBBB);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  void onTap(int value) &#123;</span><br><span class="line">    pageController.animateToPage(value,</span><br><span class="line">        duration: const Duration(milliseconds: 100), curve: Curves.ease);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>用<strong>Row</strong>组件放5个按钮品均分布，每个组件用<strong>Column</strong>包裹一个图标和一个文本，再放一个悬浮按钮在中间，<strong>shape: CircularNotchedRectangle()</strong>该属性使底部导航栏和悬浮按钮完美融合。</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>对比两种实现效果，第一种中间按钮和底部导航栏没有融合，略显死板。第二种虽然代码多一些，但是实现效果很炫酷。个人推荐第二种写法！！！</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/limh/tags/Flutter/" rel="tag"># Flutter</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/limh/2019/04/20/android03/" rel="next" title="Android原生拍照及相册选取图片">
                <i class="fa fa-chevron-left"></i> Android原生拍照及相册选取图片
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="SOHUCS"></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/limh/images/avatar.jpg" alt="土逗牛肉">
            
              <p class="site-author-name" itemprop="name">土逗牛肉</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/limh/archives/">
              
                  <span class="site-state-item-count">255</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/limh/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/limh/tags/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/limhGeek" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:1591054297@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/f73b243e7cc9" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-user-circle-o"></i>简书</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#思路"><span class="nav-number">2.</span> <span class="nav-text">思路</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#BottomNavigationBar实现"><span class="nav-number">2.1.</span> <span class="nav-text">BottomNavigationBar实现</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#BottomAppBar"><span class="nav-number">2.2.</span> <span class="nav-text">BottomAppBar</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#总结"><span class="nav-number">3.</span> <span class="nav-text">总结</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">土逗牛肉</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
	var str = GetUrlParam('height');
    if (str === '0'){
		document.getElementById('header').style.display = 'none';
		document.getElementById('comments').style.display = 'none';
		document.getElementById('footer').style.display = 'none';
		document.getElementsByClassName('post-footer').item(0).style.display = 'none'
	}
	
    function GetUrlParam(paraName) {
        var url = document.location.toString();
        var arrObj = url.split("?");

        if (arrObj.length > 1) {
            var arrPara = arrObj[1].split("&");
            var arr;

            for (var i = 0; i < arrPara.length; i++) {
                 arr = arrPara[i].split("=");

                if (arr != null && arr[0] === paraName) {
                    return arr[1];
                }
            }
            return "";
        } else {
            return "";
        }
    }
</script>









  












  
  
    <script type="text/javascript" src="/limh/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/limh/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/limh/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/limh/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/limh/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/limh/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
